﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnnotateMe.Wpf
{
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using AnnotateMe.Interfaces;
    using AnnotateMe.Logic;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DrawAnnotation();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DrawAnnotation();
        }

        private void DrawAnnotation()
        {
            var image = new Bitmap(500, 300);

            var inputs2 = new Tuple<string, string, Color?>[]
            {
               new Tuple<string, string, Color?>("find", null, null),
               new Tuple<string, string, Color?>(" ", null, null),
               new Tuple<string, string, Color?>("best", "business adjective", Color.Plum),
               new Tuple<string, string, Color?>(" ", null, null),
               new Tuple<string, string, Color?>("pizza", "business category", Color.LightCoral),
               new Tuple<string, string, Color?>(" ", null, null),
               new Tuple<string, string, Color?>("in", "preposition preposition preposition", Color.Navy),
               new Tuple<string, string, Color?>(" ", null, null),
               new Tuple<string, string, Color?>("seattle", "location", Color.MediumSeaGreen),
            };

            var inputs3 = new Tuple<string, string, Color?>[]
            {
               new Tuple<string, string, Color?>("lj-user", "агентурное задание", Color.Plum),
               new Tuple<string, string, Color?>(" ", null, null),
               new Tuple<string, string, Color?>(" ", null, null),
               new Tuple<string, string, Color?>("naval", "род войск", Color.Navy),
               new Tuple<string, string, Color?>("ny", "порт приписки", Color.LightCoral),
            };

            const string Error = "I'm afraid I can't do that.";

            var graphics = Graphics.FromImage(image);
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            var colorProvider = new RandomColorProvider();
            var queryParser = new CaretDelimitedQueryParser();

            //var tuples = queryParser.Parse(@"(lj-user)^(легенда)  (naval)^(род войск)(ny)^(порт приписки)");
            //var tuples = queryParser.Parse(@"(lj-user)^(легенда)  (naval)^(род войск)(ny)^(порт приписки) -- ^ 4 \\ \^ (test\()^(\)\\\\)");
            // var tuples = queryParser.Parse(@"_____  ((_____)^(label))\^((_____)^(annotation)) ____");
            var tuples = queryParser.Parse(@"((label)^(annotation))\^(annotation)");
            var inputs = tuples.Select(t => new Tuple<string, string, Color?>(t.Item1, t.Item2, (t.Item2 == string.Empty) ? (Color?)null : colorProvider.GetNextColor()));

            var annotator = new SimpleAnnotator();
            var layoutService = new SimpleLayoutService((startPoint, endPoint) => new LineSegmentConnector(startPoint, endPoint, (sPoint, ePoint) => graphics.DrawLine(Pens.Black, sPoint.ToPointF(), ePoint.ToPointF())));

            var shape = layoutService.Arrange(annotator.CreateBlocks(inputs, new Font("Consolas", 16), new Font("Verdana", 8), graphics).ToList());
            shape.Draw();
            
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Png);
            ms.Position = 0;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();

            this.myImage.Source = bi;
        }
    }
}
